package com.bluesmart.mia;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class NoteSleepActivity extends BaseActivity implements OnClickListener{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_note_sleep);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.note));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.text0).setOnClickListener(this);
		findViewById(R.id.text1).setOnClickListener(this);
		initDate();
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.left) finish();
		else if(arg0.getId() == R.id.right) sendHttpRequest();
		else showDate(arg0.getId());
	}
	
	void showDate(final int index)
	{
/*		Calendar calendar = Calendar.getInstance();
		
		TimePickerDialog dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				String text = "";
				if(arg1 < 10) text = "0";
				text += arg1;
				text += ":";
				if(arg2 < 10) text += "0";
				text += arg2;
				
				((TextView)findViewById(index)).setText(text);
            }
        }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),true);
		dialog.setTitle("");
		dialog.show();*/
		
		showImagePicker(index);
	}
	
	private void sendHttpRequest() {
		String text0 = ((TextView)findViewById(R.id.text0)).getText().toString();
		String text1 = ((TextView)findViewById(R.id.text1)).getText().toString();
		
		if(text0.length() == 0 || text1.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000"))
				{
					List<Activity> list = ((DemoApp) getApplicationContext()).getActivityList();
					((NoteViewActivity)list.get(list.size() - 2)).sendChartHttpRequest(true);
					
					finish();
					ToastUtil.showToastWithAlertPic(R.string.success);
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", ((DemoApp)getApplicationContext()).getSeletedBaby().getString("babyid"));
		params.put("notetype", "" + getIntent().getExtras().getInt("type"));
		params.put("handtype", "add");
		params.put("content", text0 + "-" + text1);

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/NoteHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	String[] yearStr;
	String[] hourStr = new String[24];
	String[] minStr = new String[60];
	void initDate()
	{
		new Thread()
		{
			public void run()
			{
				Calendar cal = Calendar.getInstance();
				cal.setTime(new Date(System.currentTimeMillis()));
				int now = cal.get(Calendar.DAY_OF_YEAR);
				
				Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
		        cal.setTime(firstDay);
		        
		        int day = now - cal.get(Calendar.DAY_OF_YEAR);
		        yearStr = new String[day + 1];
		        
				for(int i = 0; i <= day; i++)
				{
					if(i < 24) hourStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.hour);
					if(i < 60) minStr[i] = String.valueOf(i<10?("0" + i):i) + getString(R.string.min);

			        yearStr[i] = Utils.ConverToString(cal.getTime(), "MM-dd EE");
			        cal.add(Calendar.DAY_OF_MONTH, 1);
				}
			}
		}.start();
	}
	
	void showImagePicker(final int index)
	{
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(arg0.getId() == R.id.btn0) mDialog.dismiss();
				else if(arg0.getId() == R.id.btn1)
				{
					WheelView year = (WheelView)mDialog.findViewById(R.id.year);
			        WheelView hour = (WheelView)mDialog.findViewById(R.id.hour);
			        WheelView min = (WheelView)mDialog.findViewById(R.id.min);
			        
			        Calendar cal = Calendar.getInstance();
					cal.setTime(new Date(System.currentTimeMillis()));
					
					Date firstDay = Utils.ConverToDate(cal.get(Calendar.YEAR) + "-01-01", "yyyy-MM-dd");
			        cal.setTime(firstDay);
			        cal.add(Calendar.DAY_OF_MONTH, year.getCurrentItem());
			        
			        String text = Utils.ConverToString(cal.getTime(), "yyyy/MM/dd");
			        text += " " + hourStr[hour.getCurrentItem()] + ":" + minStr[min.getCurrentItem()];
			        text = text.replace("时", "");
			        text = text.replace("分", "");
			        
					((TextView)findViewById(index)).setText(text);
					mDialog.dismiss();
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_date);
		
		Date date = new Date(System.currentTimeMillis());
		
        WheelView year = (WheelView)mDialog.findViewById(R.id.year);
        year.setViewAdapter(new ArrayWheelAdapter<String>(this, yearStr,Color.parseColor("#404040")));
        year.setCurrentItem(yearStr.length - 1);
		
        WheelView hour = (WheelView)mDialog.findViewById(R.id.hour);
        hour.setViewAdapter(new ArrayWheelAdapter<String>(this, hourStr,Color.parseColor("#404040")));
        hour.setCurrentItem(date.getHours());
        
        WheelView min = (WheelView)mDialog.findViewById(R.id.min);
        min.setViewAdapter(new ArrayWheelAdapter<String>(this, minStr,Color.parseColor("#404040")));
        min.setCurrentItem(date.getMinutes());
		
		mDialog.findViewById(R.id.btn0).setOnClickListener(listen);
		mDialog.findViewById(R.id.btn1).setOnClickListener(listen);
		mDialog.show();
	}
}
